define(["jquery","bbcare",
    "scprit/followup/app/datatable/datatable.servers.js",
    "scprit/followup/app/datatable/datatable.ui.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningList.ui.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningList.event.js"],function($,$bb,$datatableServers,$datatableUi,$ui,$dataDeal,$event){
    var servers = {};
    var URL = $bb.baseUrl("followup")+"followup/service";
    /**
     * 入参
     */
    var paramsDeal = function (type){
        var inParams = function(args){
            var data = JSON.stringify({
                data: {
                    "args":JSON.stringify(args),
                    'examinationPaperName':type,
                    'phoneNum' : $.trim($("#tel").val()),
                    'patientName' : $.trim($("#username").val())
                }
            });
            return data;
        };
        return inParams;
    };
    /**
     * 出参
     */
    var paramsDeal2 = function(type){
        var outParams = function(data){
            if(data.rows == 0){
                var phone = $.trim($("#tel").val());
                var name = $.trim($("#username").val());
                if(phone){
                    $("#newTel").addClass("show");
                }
                if(name){
                    $("#newName").addClass("show");
                }
            }else{
                $(".new").removeClass("show");
            }
            return data.rows;
        };
        $("#newTel").click(function(){
            var phone = $.trim($("#tel").val());
            $('#myModal').modal('show');
            $("#searchUser").val(phone);
            $event.toSearch(type,phone);
        });
        $("#newName").click(function(){
            var name = $.trim($("#username").val());
            $('#myModal').modal('show');
            $("#searchUser").val(name);
            $event.toSearch(type,name);
        });
        return outParams;
    }
    /**
     * columns
     */
    var questionAge = function(e){
        // var actualDay = $dataDeal.dateDiff(e.birthday,$dataDeal.getNowFormatDate());
        // var age = $dataDeal.age(actualDay, e.pregnancDay);
        var age = window.getAge(e.birthday, new Date(), 280 - e.pregnancDay);
        return age
    };
    var questionOpration = function(e){
        var actualDay = $dataDeal.dateDiff(e.birthday,$dataDeal.getNowFormatDate());
        var monthAge = $dataDeal.monthAge(actualDay ,e.pregnancDay);
        var str = '';
        switch(e.type) {
            case 'AIMS':
                if( monthAge*1 <= 19 ){
                    str += '<a class="newC">新做测评</a>&nbsp;&nbsp;';
                }
                return str + '<a class="history">历史测评</a>';
            case 'BSID2':
                if( monthAge*1 < 31 && monthAge*1 >= 2 ){
                    str += '<a class="newC">新做测评</a>&nbsp;&nbsp;';
                }
                return str + '<a class="history">历史测评</a>';
            case 'Temperament':
                if ( monthAge*1 < 36) {
                    str += '<a class="newC">新做测评</a>&nbsp;&nbsp;';
                }
                return str + '<a class="history">历史测评</a>';
            default: 
                return '<a class="newC">新做测评</a>&nbsp;&nbsp;<a class="history">历史测评</a>';
        }
    };
    var columnsData = function(){
        var columnsArr = ['userName','phoneNum',questionAge ,'createTime',questionOpration];
        return $datatableServers.getColumnsData(columnsArr);
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        $("#username,#tel").on("input propertychange",function(){
            $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
        });
    };
    /*
    * 行回调函数
    * */
    var rowFn = function(type){
        var rowCallback = function( row, data, index){
            $('td:last a', row).click(function(){
                var userId = data.userId;
                var attr = $(this).attr("class");
                var obj = {
                    "type":type,
                    "userId":userId
                };
                if(attr == "newC"){
                    if(type== "AIMS"){
                        $bb.openPage("albertaAssessmentOfInfants",obj);
                    }
                    if(type== "PEABODY"){
                        $bb.openPage("peabodyScale",obj);
                    }
                    if(type== "BSID"){
                        $bb.openPage("bsid3",obj);
                    }
                    if(type== "BSID2"){
                        $bb.openPage("bsid2",obj);
                    }
                    if(type == 'Temperament'){
                        $bb.openPage("temperamentAssessment", obj);
                    }
                    if(type == 'nbna'){
                        $bb.openPage("nbnaAssessment", obj);
                    }
                }else{
                     if(type== "AIMS"){
                        $bb.openPage("pregnantHistory",obj);
                    }
                    if(type== "PEABODY"){
                        $bb.openPage("peabodyHistory",obj);
                    }
                    if(type== "BSID"){
                        $bb.openPage("bsidHistory3",obj);
                    }
                    if(type== "BSID2"){
                        $bb.openPage("bsidHistory2",obj);
                    }
                    if(type == 'Temperament'){
                        $bb.openPage("temperamentAssessmentHistory", obj);
                    }
                    if(type == 'nbna'){
                        $bb.openPage("nbnaAssessmentHistory", obj);
                    }
                }
            });
        };
        return rowCallback;
    };

    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(type){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "/patient/evaluation/patientListqry?sessionId="+sessionId,
                "data": paramsDeal(type),
                "dataSrc": paramsDeal2(type)
            },
            "columns": columnsData(),
            "initComplete": initComplete,
            "rowCallback":rowFn(type)
        };
        return options;
    };
    return servers;
});